<?php
Yii::import('application.plugins.*');
class FrontendController extends BaseController
{

    public $layout = 'main';
    public $data = array();
    public $connection = null;
    public $config = array();

    public function init()
    {
        parent::init();
        $this->connection = Yii::app()->db;
        $this->startUp();
    }

    public function startUp(){
        //设置基础配置
        $cfgList = $this->connection->createCommand("select * from rk_config ")->queryAll();
        foreach ($cfgList as $cfg){
            $this->config[$cfg['cfg_type'].'-'.$cfg['cfg_key']] = $cfg['cfg_value'];
        }
    }

    public function getNavgation(){
        $list = array();
        $navgations = $this->connection->createCommand("select * from rk_navgation order by navgation_order desc")->queryAll();
        foreach ($navgations as $navgation){
            $metas = $this->connection->createCommand("select * from rk_navgationmeta where navgation_id = :navgation_id")
                ->bindParam(":navgation_id",$navgation['navgation_id'])
                ->queryAll();
            $meta_array = array();
            foreach ($metas as $meta){
                $meta_array[$meta['meta_key']] = $meta['meta_value'];
            }
            $list[] = array(
                'navgation' =>$navgation,
                'meta'=>$meta_array
            );
        }
        return $list;
    }

    public function getPostByCategory($categoryId){
        $list = array();
        $posts = $this->connection->createCommand("select * from rk_post where category_id = :category_id")
            ->bindParam(":category_id",$categoryId)
            ->queryAll();
        foreach ($posts as $post){
            $metas = $this->connection->createCommand("select * from rk_postmeta where post_id = :post_id")
                ->bindParam(":post_id",$post['post_id'])
                ->queryAll();
            $meta_array = array();
            foreach ($metas as $meta){
                $meta_array[$meta['meta_key']] = $meta['meta_value'];
            }
            $list[] = array(
                'post' =>$post,
                'meta'=>$meta_array
            );
        }
        return $list;
    }

    public function getPostByCategorys($categoryIds){
        $list = array();
        $posts = $this->connection->createCommand("select * from rk_post where category_id in (".$categoryIds.")")
            ->queryAll();
        foreach ($posts as $post){
            $metas = $this->connection->createCommand("select * from rk_postmeta where post_id = :post_id")
                ->bindParam(":post_id",$post['post_id'])
                ->queryAll();
            $meta_array = array();
            foreach ($metas as $meta){
                $meta_array[$meta['meta_key']] = $meta['meta_value'];
            }
            $list[] = array(
                'post' =>$post,
                'meta'=>$meta_array
            );
        }
        return $list;
    }

    public function getCategorys($categoryids){
        return $this->connection->createCommand("select * from rk_category where category_id in (".$categoryids.")")
            ->queryAll();
    }

    public function getPost($categoryId){
        $count = $this->connection->createCommand("select count(*) from rk_post where category_id = :category_id")
            ->bindParam(":category_id",$categoryId)
            ->queryScalar();
        $pager = new CPagination($count);
        $pager->pageSize = 1;
        $pager->currentPage = isset($_REQUEST['page'])?$_REQUEST['page']:0;

        $offset = $pager->getOffset();
        $limit = $pager->getLimit();
        $posts = $this->connection->createCommand("select * from rk_post  where  category_id = :category_id order by createtime desc limit :offset,:limit")
            ->bindParam(":category_id",$categoryId)
            ->bindParam(":offset",$offset)
            ->bindParam(":limit",$limit)
            ->queryAll();
        $list = [];
        foreach ($posts as $post){
            $metas = $this->connection->createCommand("select * from rk_postmeta where post_id = :post_id")
                ->bindParam(":post_id",$post['post_id'])
                ->queryAll();
            $meta_array = array();
            foreach ($metas as $meta){
                $meta_array[$meta['meta_key']] = $meta['meta_value'];
            }
            $list[] = array(
                'post' =>$post,
                'meta'=>$meta_array
            );
        }
        return array('postlist'=>$list,'pager'=>$pager);
    }

    public function getRecentPost($categoryId){
        $posts = $this->connection->createCommand("select * from rk_post where  category_id = :category_id  order by createtime desc  limit 0,10")
            ->bindParam(":category_id",$categoryId)
            ->queryAll();
        $list = [];
        foreach ($posts as $post){
            $metas = $this->connection->createCommand("select * from rk_postmeta where post_id = :post_id")
                ->bindParam(":post_id",$post['post_id'])
                ->queryAll();
            $meta_array = array();
            foreach ($metas as $meta){
                $meta_array[$meta['meta_key']] = $meta['meta_value'];
            }
            $list[] = array(
                'post' =>$post,
                'meta'=>$meta_array
            );
        }
        return $list;
    }

    public function getPostById($postId){
        $post = $this->connection->createCommand("select * from rk_post where  post_id = :post_id  ")
            ->bindParam(":post_id",$postId)
            ->queryRow();
        $metas = $this->connection->createCommand("select * from rk_postmeta where post_id = :post_id")
            ->bindParam(":post_id",$postId)
            ->queryAll();
        $meta_array = array();
        foreach ($metas as $meta){
            $meta_array[$meta['meta_key']] = $meta['meta_value'];
        }
        $object= array(
            'post' =>$post,
            'meta'=>$meta_array
        );
        return $object;
    }

}